package com.htzos.admin.system.service;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.htzos.admin.system.bean.SystemRoleMenu;
import com.htzos.admin.system.dao.SystemRoleMenuMapper;
import com.htzos.common.tool.ExtQueryBean;
@Service
@Transactional(rollbackFor = Exception.class)
public class SystemRoleMenuService {
    @Autowired
    SystemRoleMenuMapper systemRoleMenuMapper;
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<SystemRoleMenu> systemRoleMenuList(SystemRoleMenu systemRoleMenu, ExtQueryBean extQuery){
       return systemRoleMenuMapper.systemRoleMenuList(systemRoleMenu, extQuery);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public Integer systemRoleMenuTotal(SystemRoleMenu systemRoleMenu, ExtQueryBean extQuery){
       return systemRoleMenuMapper.systemRoleMenuTotal(systemRoleMenu, extQuery);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<SystemRoleMenu> systemRoleMenuViewList(SystemRoleMenu systemRoleMenu, ExtQueryBean extQuery){
    	return systemRoleMenuMapper.systemRoleMenuViewList(systemRoleMenu, extQuery);
    }
    
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public Integer systemRoleMenuViewTotal(SystemRoleMenu systemRoleMenu, ExtQueryBean extQuery){
    	return systemRoleMenuMapper.systemRoleMenuViewTotal(systemRoleMenu, extQuery);
    }
    
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public int systemRoleMenuUpdate(SystemRoleMenu systemRoleMenu){
       return systemRoleMenuMapper.systemRoleMenuUpdate(systemRoleMenu);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public int systemRoleMenuInsert(SystemRoleMenu systemRoleMenu){
       return systemRoleMenuMapper.systemRoleMenuInsert(systemRoleMenu);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public int systemRoleMenuDelete(SystemRoleMenu systemRoleMenu){
       return systemRoleMenuMapper.systemRoleMenuDelete(systemRoleMenu);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public SystemRoleMenu systemRoleMenuGet(String id){
       return systemRoleMenuMapper.systemRoleMenuGet(id);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<SystemRoleMenu> systemRoleMenuListByUser(Integer userId){
       return systemRoleMenuMapper.systemRoleMenuListByUser(userId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public int systemRoleMenuSetRight(Integer roleId, Integer menuId, String right, boolean checked){
		SystemRoleMenu systemRoleMenu = new SystemRoleMenu();
		systemRoleMenu.setRoleId(roleId);
		systemRoleMenu.setMenuId(menuId);
		List<SystemRoleMenu> list = systemRoleMenuMapper.systemRoleMenuList(systemRoleMenu, null);
		if(list.size() > 0){
			//权限存在，更新权限设置
			systemRoleMenu = list.get(0);
		}
		if(right.equals("rselect")){
			systemRoleMenu.setRselect(checked ? 1 : 0);
		}
		else if(right.equals("radd")){
			systemRoleMenu.setRadd(checked ? 1 : 0);
		}
		else if(right.equals("rupdate")){
			systemRoleMenu.setRupdate(checked ? 1 : 0);
		}
		else if(right.equals("rdelete")){
			systemRoleMenu.setRdelete(checked ? 1 : 0);
		}
		else{
			return 0;
		}
		
		if(systemRoleMenu.getId() != null){
			//权限存在，更新权限设置
			//如果4种权限都是没权限状态，则删除这个菜单权限
			if((systemRoleMenu.getRselect() + systemRoleMenu.getRadd() + systemRoleMenu.getRupdate() + systemRoleMenu.getRdelete()) == 0){
				return systemRoleMenuMapper.systemRoleMenuDelete(systemRoleMenu);
			}
			else{
				return systemRoleMenuMapper.systemRoleMenuUpdate(systemRoleMenu);
			}
		}
		else{
			//新建
			return systemRoleMenuMapper.systemRoleMenuInsert(systemRoleMenu);
		}
    }
}